﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EventReminder.Common;
using System.Net.Mail;
using System.ServiceModel;

namespace EventReminder.Server
{
    public class GodObject
    {
        TaskEventSystem currentevent;
        List<string> allEvents;
        UsersManager userlist;
        EventBuilder newevent;
        Timer eventTimer; // if this doesn't work now, use System.Threading.Timer
        ServiceHost _host;
        IEventProcessor _eventProcessor;
        

        // update lstUsers
        public List<string> Users 
        { 
            get 
            {
                List<string> userdata = userlist.GetUserList();
                
                return userdata;  
            }  
        }
        // update lstAllEvents
        public List<string> AllEvents
        {
            get
            {
                List<string> userdata = new List<string>();
                userdata = newevent.DisplayAllData();

                return userdata;
            }
        }

        public GodObject()
        {
            currentevent = new TaskEventSystem();
            newevent = new EventStart();

            currentevent.BuildMessageSystem(newevent);
            this.eventTimer = new System.Windows.Forms.Timer();
            this.eventTimer.Interval = 30000;
            this.eventTimer.Tick += new System.EventHandler(this.eventTimer_Tick);

            ServiceHost host = new ServiceHost(typeof(EventReminderService));

            EventProcessorBuilder builder = new EventProcessorBuilder();
            builder.AddLink(new DefaultEventProcessor());
            builder.AddLink(new UnknownProcessor());
            builder.AddLink(new MeetingProcessor());
            builder.AddLink(new EventProcessor());
            builder.AddLink(new TaskingFollowupProcessor());
            _eventProcessor = builder.GetTopLinkOfChain();
        }

        public void Load()
        {
            userlist = new UsersManager(); // populates self in constructor
            allEvents = newevent.DisplayAllData();

            List<string> AllUserData = userlist.GetUserList();

            eventTimer.Enabled = true;
        }

        public void OpenService()
        {
            _host.Open();
        }

        public void CloseService()
        {
            eventTimer.Enabled = false;
            _host.Close();
            
        }

        private void eventTimer_Tick(object sender, EventArgs e)
        {
            //Check for New Messages
            newevent.EventRefresh();
            
            //Check for Messages to send
            newevent.MarkMessagesToSend();
            //Check Send messages
            List<string> messagelist = newevent.GetMessagesToSend();
            foreach (string s in messagelist)
            {
                //SendEmailMessage(s);
            }
            //Archive Messages
        }

        private void SendEmailMessage(string data)
        {
            string[] dataline = data.Split('|');

            try
            {
                //Mail Message
                MailMessage mM = new MailMessage();
                //Mail Address
                mM.From = new MailAddress("erasse658sp2012@gmail.com");
                //receiver email id
                mM.To.Add(dataline[3]);
                //subject of the email
                mM.Subject = dataline[1];
                //deciding for the attachment
                //mM.Attachments.Add(new Attachment(@"C:\\attachedfile.jpg"));
                //add the body of the email
                mM.Body = dataline[2];
                mM.IsBodyHtml = true;
                //SMTP client
                SmtpClient sC = new SmtpClient("smtp.gmail.com");
                //port number for Gmail mail
                sC.Port = 587;
                //credentials to login in to Gmail account
                sC.Credentials = new System.Net.NetworkCredential("erasse658sp2012@gmail.com", "mercermastersprogram");
                //enabled SSL
                sC.EnableSsl = true;
                //Send an email
                sC.Send(mM);
            }//end of try block
            catch (Exception ex)
            {

            }//end of catch
        }

        public void AddEvent(EventData data)
        {
            _eventProcessor.AddEvent(data);
        }

        public void ModifyEvent(EventData oldVersion, EventData newVersion)
        {
            _eventProcessor.ModifyEvent(oldVersion, newVersion);
        }

        public void DeleteEvent(EventData data)
        {
            _eventProcessor.DeleteEvent(data);
        }
    }
}
